package com.app.newiptv.presenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.bean.ChannelType;
import com.app.newiptv.bean.Content;
import com.app.newiptv.bean.VodItemBean;
import com.app.newiptv.bean.VodListBean;
import com.app.newiptv.bean.details.PlayDetailsBean;
import com.app.newiptv.callback.ContentCallback;
import com.app.newiptv.callback.IRequestCallback;
import com.app.newiptv.service.AHservice;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.Paths;

import java.nio.charset.IllegalCharsetNameException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static android.content.ContentValues.TAG;


/**
 * Created by wendal on 2017/9/15.
 */

public class VodPresenter {
    private boolean isDisplayed;
    private Callback callback;
    private List<Content> listContent;
    private List<Content> vodContent;
    List<ChannelType.DataBean> dataBeanList; // 左侧的数据
    List<VodItemBean.VodItemInfo> mVodItemInfoList; // 右侧的数据
    private VodListBean vodListBean;
    private List<PlayDetailsBean> detailsBeanList;
    private AHservice service;
    private Context mContext;
    private String bigType;
    private String currentType;

    public VodPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(VodPresenter presenter, int count);

        void onVodLoadCompleted(VodPresenter presenter, int count);

        void onDetailCompleted(VodPresenter presenter);
    }

    public interface ViewHolder {
        void setName(String name);
    }

    public interface ItemViewHolder {
        void setImage(String imageUrl);

        void setItemName(String name);
        void setItemNewShow(Boolean show);
    }

    public interface IDetailDataViewHolder {
        void setNameAndCount(String name, String count);
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    public void setBigType(String bigType) {
        this.bigType = bigType;
    }

    //加载数据
    public void loadItem(String url, Callback callback) {
        this.callback = callback;
        service = new AHservice();
        service.loadVodListData(url, new ItemLoadCallback());
    }

    // 右侧列表数据
    public void loadVodItem(int postion) {
        //service.loadVodItemData("", new VodItemLoadCallBack());
//        if (vodListBean != null && vodListBean.getChildren() != null && vodListBean.getChildren().size() > 0 && postion < vodListBean.getChildren().size()) {
//            detailsBeanList = vodListBean.getChildren().get(postion).getVideos();
//            if (detailsBeanList != null) {
//                callback.onVodLoadCompleted(VodPresenter.this, detailsBeanList.size());
//            }
//        }
        List<ChannelType.DataBean.CsBean> csBeans = getMoviceClassDatas();
        if (isDataBeanListUse(csBeans, postion)) {
            String type = csBeans.get(postion).getT_id();
            currentType = type;
            service = new AHservice();
            Log.i(TAG, "loadVodItem: " + Paths.HOSTPATH+Paths.CHANNLE_LIST + "?t=" + type);
            service.loadVodItemData(Paths.HOSTPATH+Paths.CHANNLE_LIST + "?t=" + type, new ItemInfoListLoadCallback());
        }
    }
    public void loadPageVodItem(int postion){
        service = new AHservice();
        service.loadVodItemData(Paths.HOSTPATH+Paths.CHANNLE_LIST + "?t=" + currentType + "&page=" + postion, new ItemInfoListLoadCallback());
    }

    public void onBindDetailData(IDetailDataViewHolder viewHolder) {

    }

    public void setPosition(int position) {
        if (mVodItemInfoList != null && position < mVodItemInfoList.size()) {
            VodItemBean.VodItemInfo latelyBean = mVodItemInfoList.get(position);
            Intent intent = new Intent();
            if(getVodType(position)){
                intent.setClass(mContext, FilmDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id",Integer.valueOf(latelyBean.getVod_id()));
                intent.putExtra("bundle",bundle);
                mContext.startActivity(intent);
            }else if(getTelType(position)){
                intent.setClass(mContext, SitcomActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id",Integer.valueOf(latelyBean.getVod_id()));
                intent.putExtra("bundle",bundle);
                mContext.startActivity(intent);
            }
        }
    }

    public void onBindMovieView(ViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
//        if (vodListBean != null && vodListBean.getChildren() != null && vodListBean.getChildren().size() > 0 && position < vodListBean.getChildren().size()) {
//            holder.setName(vodListBean.getChildren().get(position).getName());
//        }
        // TODO:hailong.qiu
        List<ChannelType.DataBean.CsBean> csBeans = getMoviceClassDatas();
        if (isDataBeanListUse(csBeans, position)) {
            holder.setName(csBeans.get(position).getT_name());
        }
    }

    /**
     * 根据HomeActivity传入的bitType获取对应的数据.
     * @return
     */
    private List<ChannelType.DataBean.CsBean> getMoviceClassDatas() {
        if (null != dataBeanList) {
            for (ChannelType.DataBean dataBean : dataBeanList) {
                if (!TextUtils.isEmpty(bigType) && dataBean.getT_id().equals(bigType)) {
                    return dataBean.getCs();
                }
            }
        }
        return null;
    }

    /**
     * 判断数据是否可用
     * @param position
     * @return
     */
    private boolean isDataBeanListUse(final List<ChannelType.DataBean.CsBean> csBeans, final int position) {
        return null != csBeans && csBeans.size() > 0 && position < csBeans.size();
    }

    public int getMovieItemCount() {
        List<ChannelType.DataBean.CsBean> csBeans = getMoviceClassDatas();
        return null != csBeans ? csBeans.size() : 0;
    }

    public void onBindItemInfo(ItemViewHolder viewHolder, int position) {
        if (!isDisplayed) {
            return;
        }

        // TODO:hailong.qiu
        if (isItemInfoListUse(position)) {
            VodItemBean.VodItemInfo vodItemInfo = mVodItemInfoList.get(position);
            if (null != vodItemInfo) {
                viewHolder.setImage(vodItemInfo.getVod_pic());
                viewHolder.setItemName(vodItemInfo.getVod_name());
                viewHolder.setItemName(vodItemInfo.getVod_name());
                String vod_time = vodItemInfo.getVod_time().split(" ")[0].trim();
                String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())).trim();
                viewHolder.setItemNewShow(vod_time.equals(today));
            }
        }
    }

    private boolean isItemInfoListUse(int position) {
        return null != mVodItemInfoList && mVodItemInfoList.size() > 0 && position < mVodItemInfoList.size();
    }

    /**
     * 获取右边的数据数目
     * @return
     */
    public int getVodItemInfoCount() {
        return null != mVodItemInfoList ? mVodItemInfoList.size() : 0;
//        return 50; //null != mVodItemInfoList ? mVodItemInfoList.size() : 0;
    }

    class ItemLoadCallback implements IRequestCallback {
        @Override
        public void onFailed(String arg0, String arg1) {
        }

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
            if (callback == null || !isDisplayed) {
                APPLog.printInfo("ItemLoadCallback onLoadComplete:callback :");
                return;
            }

//            if (dto != null && dto instanceof VodListBean) {
//                vodListBean = (VodListBean) dto;
//                callback.onMovieCompleted(VodPresenter.this, vodListBean.getChildren().size());
//            }
            // TODO: hailong.qiu
            if (null != dto && dto instanceof List) {
                dataBeanList = (List<ChannelType.DataBean>) dto;
                callback.onMovieCompleted(VodPresenter.this, getMovieItemCount());
            }
        }

        @Override
        public void onLoading(String arg0) {
        }
    }

    class ItemInfoListLoadCallback implements IRequestCallback {
        @Override
        public void onFailed(String arg0, String arg1) {
        }

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
            if (callback == null || !isDisplayed) {
                APPLog.printInfo("ItemInfoListLoadCallback onLoadComplete:callback :");
                return;
            }
            // TODO: hailong.qiu
            if (null != dto && dto instanceof VodItemBean.VodDataBean) {
                VodItemBean.VodDataBean vodDataBean = (VodItemBean.VodDataBean) dto;
                if (null != vodDataBean) {
                    mVodItemInfoList = vodDataBean.getList();
                    if (null != callback) {
                        callback.onVodLoadCompleted(VodPresenter.this, getVodItemInfoCount());
                    }
                }
            }
        }

        @Override
        public void onLoading(String arg0) {
        }
    }

    class VodItemLoadCallBack implements ContentCallback {

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, List<Content> contents) {
            vodContent = contents;
            callback.onVodLoadCompleted(VodPresenter.this, count);
        }

        @Override
        public void onLoading(String contentID) {

        }

        @Override
        public void onFailed(String code, String msg) {

        }
    }

    public boolean getVodType(int position) {
        VodItemBean.VodItemInfo playDetailsBean = mVodItemInfoList.get(position);
        if(Integer.valueOf(playDetailsBean.getType_pid()) == 1){
            return true;
        }
        return false;
    }

    public boolean getTelType(int position) {
        VodItemBean.VodItemInfo playDetailsBean = mVodItemInfoList.get(position);
        if(Integer.valueOf(playDetailsBean.getType_pid()) != 1){
            return true;
        }
        return false;
    }

}
